package com.telecom.cms.dao;
// default package

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.*;
import com.telecom.cms.util.Constant;

/**
 * Data access object (DAO) for domain model class CampLeftMenuGroup.
 * 
 * @see .CampLeftMenuGroup
 * @author MyEclipse Persistence Tools
 */

public class CampLeftMenuGroupDAO {
	private static List instlist=null;
	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampLeftMenuGroup transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".CAMP_LEFT_MENU_GROUP where Menu_Group_Id="
				+ transientInstance.getMenuGroupId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".CAMP_LEFT_MENU_GROUP set m_menu_id=" 
						+ transientInstance.getMMenuId()+",Menu_Group_Name='"
						+ transientInstance.getMenuGroupName() + "',Menu_Group_Src_Type='"
						+ transientInstance.getMenuGroupSrcType()+ "',Menu_Group_Url='"
						+ transientInstance.getMenuGroupUrl()+"', Group_Show_Order="
						+ transientInstance.getGroupShowOrder()+" where Menu_Group_Id="
			        	+ transientInstance.getMenuGroupId();
			else
				sql = "insert into " + db + ".CAMP_LEFT_MENU_GROUP values(" 
						+ transientInstance.getMenuGroupId()+","
				        + transientInstance.getMMenuId() + " , '"
						+ transientInstance.getMenuGroupName() + "' , '"
						+ transientInstance.getMenuGroupSrcType() + "','"
						+ transientInstance.getMenuGroupUrl() + "',"
						+ transientInstance.getGroupShowOrder() + ")";
			rs = dao.insert(sql);
			if (rs2 != null) {
				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		findAllForce();
		return rs;
	}
	
	public void delete(CampLeftMenuGroup persistentInstance) {
		String sql = "delete from " + db + ".CAMP_LEFT_MENU_GROUP where Menu_Group_Id="
				+ persistentInstance.getMenuGroupId();
		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		findAllForce();
	}
	
	public List findAllForce() {
		String sql = "select * from " + db + ".CAMP_LEFT_MENU_GROUP order by Group_Show_Order ";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampLeftMenuGroup sd = new CampLeftMenuGroup();
				sd.setMenuGroupId(rs.getInt(1));
				sd.setMMenuId(rs.getInt(2));
				sd.setMenuGroupName(rs.getString(3));
				sd.setMenuGroupSrcType(rs.getString(4));
				sd.setMenuGroupUrl(rs.getString(5));
				sd.setGroupShowOrder(rs.getInt(6));
				list.add(sd);
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		instlist=list;
		return list;
	}
	
	public List findAll() {
		if(instlist==null)
			findAllForce();
		return instlist;
	}
	
	public List  findByMenuId(int mmenuid){
		//String sql = "select * from " + db + ".CAMP_LEFT_MENU_GROUP where m_Menu_id="+mmenuid+"  order by Group_Show_Order ";
		List list = new ArrayList();
		if(instlist==null)
			findAllForce();
		for(int i=0;i<instlist.size();i++)
		{
			CampLeftMenuGroup sd = (CampLeftMenuGroup)instlist.get(i);
			if(sd.getMMenuId()==mmenuid)
				list.add(sd);
		}
		/*try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampLeftMenuGroup sd = new CampLeftMenuGroup();
				sd.setMenuGroupId(rs.getInt(1));
				sd.setMMenuId(rs.getInt(2));
				sd.setMenuGroupName(rs.getString(3));
				sd.setMenuGroupSrcType(rs.getString(4));
				sd.setMenuGroupUrl(rs.getString(5));
				sd.setGroupShowOrder(rs.getInt(6));
				list.add(sd);
			}
			if (rs != null) {
				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}*/
		return list;
	}
}